﻿@{
    ViewBag.Title = Model == null ? "新建基础保险产品" : "编辑基础保险产品";
    var id = Model != null ? Model.ID : 0;
}
@using ZBJF.Warranty.BLL
@using ZBJF.Warranty.Model.ViewModel.AuditConfig
@model ZBJF.Warranty.Model.InsuranceProducts
<style>
    #PolicyPeriodYear,#PolicyPeriodMonth,#PolicyPeriodDay {
    width:30px;
    }
</style>

<ul class="nav nav-tabs">
    <li class="active"><a href="/InsuranceProducts/AddInsuranceProductsBase/@id">@(ViewBag.Title)</a></li>
</ul>
<h4> &nbsp; &nbsp;产品基本信息</h4>
@Html.HiddenFor(m => m.ID)
<table class="table table-responsive table-bordered table-hover baseTable">
    <tr>
        <th width="100">产品名称：</th>
        <td width="200">
            @Html.TextBoxFor(m => m.ProductName, new { maxlength = 15, validate = "{required:true,messages:{required:'请输人产品的名称'}}" })
        </td>
        <th width="100">保额：</th>
        <td width="200">
            @Html.TextBoxFor(m => m.Coverage, new { maxlength = 8, @class = "input-small", onkeyup = "onlyInputDecimal(this)", validate = "{required:true,messages:{required:'请输入保额！'}}" }) 元
        </td>
        <th width="100">生效等待期：</th>
        <td width="200">
            @Html.TextBoxFor(m => m.WaitDays, new { maxlength = 8, @class = "input-small", onkeyup = "onlyInputInt(this)", validate = "{required:true,messages:{required:'请输入生效等待期！'}}" }) 天
        </td>
        <th width="100">保险期限：</th>
        <td width="400">
            @Html.TextBoxFor(m => m.PolicyPeriodYear, new { maxlength = 4, @class = "input-small policyPeriod", onkeyup = "onlyInputInt(this)", validate = "{required:true,messages:{required:'请输入保额！'}}" }) 年
            @Html.TextBoxFor(m => m.PolicyPeriodMonth, new { maxlength = 4, @class = "input-small policyPeriod", onkeyup = "onlyInputInt(this)", validate = "{required:true,messages:{required:'请输入保额！'}}" }) 月
            @Html.TextBoxFor(m => m.PolicyPeriodDay, new { maxlength = 4, @class = "input-small policyPeriod", onkeyup = "onlyInputInt(this)", validate = "{required:true,messages:{required:'请输入保额！'}}" }) 日
        </td>
    </tr>
</table>
<!--已添加服务列表-->
<p class="group-header ">已添加服务列表<a class="btn btn-info margin-10" href="javascript:void(0)" onclick="addServiceProject()" id="addServiceProjectBtn">添加服务项目</a></p>
<table class="table table-responsive table-bordered table-hover">
    <thead>
        <tr>
            <th>服务类型</th>
            <th>服务项目</th>
            <th>服务次数</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody id="serviceProjectList">
        <tr class="noService">
            <td colspan="55">暂无服务项目</td>
        </tr>
    </tbody>
</table>


<div style="text-align: center;">
    <input id="btnAuditSave" class="btn btn-primary Edit-R" type="button" value="保存" onclick="submitForm(this)" />
    <button id="btnClose" class="btn btn-primary Edit-R" type="reset" onclick="window.history.back(-1)">
        关闭
    </button>
</div>

<div id="cellPhoneRepairService" style="position:absolute;left:0;top:0; background-color:#fff;width:100%; min-height:500px; display:none">
    @Html.Action("CellPhoneRepairService")
</div>
@section scripts
{
    <script type="text/javascript">
        window.onload = function () {
            baseProductOnload();
            cellPhoneOnload();
        }

        //基础产品页面加载完执行
        function baseProductOnload()
        {
            let _id = $("#ID").val();
            if (_id) {
                //获取服务项目列表
                getServiceProjectList(_id);
            }
        }

        //点击添加服务项目
        function addServiceProject() {
            resetCellPhoneFrom();
            $("#cellPhoneRepairService").show();
        }
        //编辑某个服务项目
        function editServiceProject(_this, projectType) {
            var _data = $(_this).parent("td").parent("tr").attr("data");
            //手机服务
            if (projectType == "cellPhone") {
                setCellPhoneFrom(JSON.parse(_data), $(_this).parent("td").parent("tr").index());
            }
        }
        //删除某个服务项目
        function deleteServiceProject(_this, projectType) {
            if (projectType == "cellPhone") {
                $(_this).parent("td").parent("tr").remove();
            }
        }
        //表单提交
        function submitForm(_this) {
            let formData = {};

            formData.ID = $("#ID").val();

            formData.ProductName = $("#ProductName").val();
            if (!formData.ProductName) {
                layer.alert("请输入产品名称");
                return;
            }
            formData.Coverage = $("#Coverage").val();
            if (!formData.Coverage) {
                layer.alert("请输入保额");
                return;
            }
            formData.WaitDays = $("#WaitDays").val();
            if (!formData.WaitDays) {
                layer.alert("请输入生效等待期");
                return;
            }
            formData.PolicyPeriodYear = $("#PolicyPeriodYear").val();
            formData.PolicyPeriodMonth = $("#PolicyPeriodMonth").val();
            formData.PolicyPeriodDay = $("#PolicyPeriodDay").val();
            if (!formData.PolicyPeriodYear && !formData.PolicyPeriodMonth && !formData.PolicyPeriodDay) {
                layer.alert("请填写保险期限");
                return;
            }
            formData.CellPhoneServiceProject = [];
            let serviceProjectItem = $(".serviceProjectItem");
            if (!serviceProjectItem) {
                layer.alert("请至少添加一个服务项目");
                return;
            }
            for (let i = 0, j = serviceProjectItem.length; i < j; i++) {
                formData.CellPhoneServiceProject.push(JSON.parse($(serviceProjectItem[i]).attr("data")));
            }
            _this.disabled = 'disabled';
            $.ajax({
                url: "/InsuranceProducts/SaveInsuranceProductsBase",
                type: "post",
                data: formData,
                success: function (data) {
                    console.log(data);
                    if (data.Result) {
                        layer.alert(data.Message, function () {
                            window.location.href = "/InsuranceProducts/Index";
                        });
                    } else {
                        layer.alert(data.Message);
                        _this.disabled = '';
                    }
                }
            })
        }

        //获取服务项目列表
        function getServiceProjectList(_id) {
            $.ajax({
                type: "GET",
                url: "/InsuranceProducts/GetProductServiceProjectList",
                dataType: "json",
                data: { id: _id },
                success: function (data) {
                    if (!data.Result) {
                        alert("初始化数据失败");
                        return;
                    }
                    debugger;
                    let cellPhone = data.Data.CellPhoneServiceProject;
                    if (cellPhone && cellPhone.length > 0) {
                        for (let i = 0, j = cellPhone.length; i < j; i++) {
                            let cellPhoneItem = cellPhone[i];
                            //生成设备等级tabled Tr
                            if (cellPhoneItem.AccessoriesLevelList) {
                                let accessoriesLevelHtml = "";
                                for (let m = 0, n = cellPhoneItem.AccessoriesLevelList.length; m < n; m++) {
                                    let accessoriesLevelItem = cellPhoneItem.AccessoriesLevelList[m];
                                    accessoriesLevelHtml += "<tr class=\"accessoriesLevelList\" datavalue=\"" + accessoriesLevelItem.ProductTypeID + "|" + accessoriesLevelItem.AccessoriesTypeID + "|" + accessoriesLevelItem.AccessoriesLevelID + "\">";
                                    accessoriesLevelHtml += "<td>" + (m + 1) + "</td>";
                                    accessoriesLevelHtml += "<td>" + accessoriesLevelItem.AccessoriesTypeName + "</td>";
                                    accessoriesLevelHtml += "<td>" + accessoriesLevelItem.AccessoriesLevelName + "</td>";
                                    accessoriesLevelHtml += "<td><a href=\"#\" onclick=\"deleteAccessoriesLevel(this)\">删除</a></td>";
                                    accessoriesLevelHtml += "</tr>";
                                }
                                cellPhoneItem.AccessoriesLevelHtml = accessoriesLevelHtml
                            }
                            //生成型号 select option
                            if (cellPhoneItem.InsuranceProductsAvailableModelTypeModel) {
                                let modelTypeHtml = "";
                                cellPhoneItem.InsuranceProductsAvailableModelType = [];
                                for (let m = 0, n = cellPhoneItem.InsuranceProductsAvailableModelTypeModel.length; m < n; m++) {
                                    let modelTypeItem = cellPhoneItem.InsuranceProductsAvailableModelTypeModel[m];
                                    cellPhoneItem.InsuranceProductsAvailableModelType.push(modelTypeItem.ModelID);
                                    modelTypeHtml += "<option value=\"" + modelTypeItem.ModelID + "\" productbrandsid=\"" + modelTypeItem.BrandID + "\" productmodelsname=\"" + modelTypeItem.ModelName + "\">" + modelTypeItem.BrandName + "_" + modelTypeItem.ModelName + "</option>";
                                }
                                cellPhoneItem.ModelsHtml = modelTypeHtml
                            }
                            let _html = setServiceProjectHtml(cellPhoneItem);

                            $("#serviceProjectList").append(_html).find(".noService").hide();
                        }
                    }

                }
            });
        }
    </script>
}
